package com.css.openapi.controller;

import cn.hutool.core.util.IdUtil;
import com.css.common.core.constant.ApiAccess;
import com.css.common.core.constant.Result;
import com.css.common.core.utils.RedisUtils;
import com.css.openapi.BO.*;
import com.css.openapi.BO.lypt.BdKpyReq;
import com.css.openapi.BO.ShbdbhReq;
import com.css.openapi.feign.RlsbCallbackCommonClient;
import com.css.openapi.feign.YljyCommonClient;
import com.css.openapi.feign.dto.*;
import com.css.openapi.service.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 外系统接入控制器
 * </p>
 *
 * @author zhuowugong
 * @since 2024-08-08 09:58:16
 */
@Slf4j
@Api(tags = "管理Api")
@RestController
@RequestMapping("/gl")
public class GlApiController {
    @Resource
    private IQyYwlxService qyYwlxService;
    @Resource
    private IQyxxKzService qyxxKzService;
    @Resource
    private LqsqWxtjrService lqsqWxtjrService;
    @Resource
    private IQyKpySqService qyKpySqService;
    @Resource
    private ApiGlService apiGlService;
    @Resource
    private ApiZrrService apiZrrService;

    @Resource
    private YljyCommonClient yljyCommonClient;

    @Resource
    private RlsbCallbackCommonClient rlsbCallbackCommonClient;
    
    /**
     * 商户绑定申请
     *
     * @param shbdReq 参数
     * @return 商户绑定信息
     */
    @PostMapping("/shbdsq")
    @ApiAccess
    public Result<ShbdBO> shbdsq(@RequestBody @Valid ShbdReq shbdReq) {
        return Result.ok(qyYwlxService.bingYwlx(shbdReq));
    }

    /**
     * 商户信息登记
     *
     * @param req 参数
     * @return 商户绑定信息
     */
    @PostMapping("/shxxDj")
    @ApiAccess
    public Result shxxdj(@RequestBody @Valid ShxxdjReq req) {
        return qyxxKzService.shxxdj(req);
    }


    /**
     * 查询商户绑定状态
     *
     * @param request 参数
     * @return 商户绑定信息
     */
    @PostMapping("/queryShbdzt")
    @ApiAccess
    public Result<Map<String, String>> queryShbdzt(@RequestBody @Valid ShbdztQueryRequest request) {
        return Result.ok(qyYwlxService.queryShbdzt(request));
    }

    /**
     * 商户解绑申请
     */
    @PostMapping("/shjbSq")
    @ApiAccess
    public Result shjbSq(@RequestBody @Valid ShbdztQueryRequest request) {
        qyYwlxService.unBindYwlx(request);
        return Result.ok();
    }

    /**
     * 获取授权码
     */
    @PostMapping("/getSqm")
    @ApiAccess
    public Result<SqmVO> getSqm(@RequestBody @Valid LqsqWxtjrReq param) {
        return Result.ok(this.lqsqWxtjrService.getSqm(param));
    }

    /**
     * 获取商户授权状态（乐企接入状态，白名单开通状态，授权认证信息）
     */
    @PostMapping("/getShlqsqzt")
    @ApiAccess
    public Result<YlwebQydglWxtjrResponse> getShlqsqzt(@RequestBody @Valid LqsqWxtjrReq param) {
        return Result.ok(this.lqsqWxtjrService.getShlqsqzt(param));
    }

    /**
     * 绑定开票员
     *
     * @param req 参数
     * @return 开票员信息
     */
    @PostMapping("/bdKpy")
    @ApiAccess
    public Result<BdKpyVO> bdKpy(@RequestBody BdKpyReq req) {
        return qyKpySqService.bdKpy(req);
    }


    /**
     * 查询商户开票员(乐企开票员)
     *
     * @param req 参数
     * @return 商户绑定信息
     */
    @PostMapping("/queryKpy")
    @ApiAccess
    public Result queryKpy(@RequestBody Map<String, String> req) {
        Map<String, Object> repMap = new HashMap<>();
        repMap.put("kpyxx", qyKpySqService.queryKpy(req.get("shbdbh")));
        return Result.ok(repMap);
    }

    /**
     * 解绑开票员
     */
    @PostMapping("/jbKpy")
    @ApiAccess
    public Result jbKpy(@RequestBody Map<String,String> req) {
        qyKpySqService.jbKpy(req);
        return Result.ok();
    }



    /**
     * 商户余额查询
     */
    @PostMapping("/queryYe")
    @ApiAccess
    public Result<Map<String,Object>> queryYe(@Validated @RequestBody ShbdbhReq req) {
        return this.apiGlService.queryYe(req);
    }

    /**
     * 商户提现
     * @param req shbdbh,txje
     * @return 返回
     */
    @PostMapping("/withDrawals")
    @ApiAccess
    public Result<Map<String,Object>> withDrawals(@Valid @RequestBody ShzhtxReq req) {
        return this.apiGlService.withdrawals(req);
    }

    /**
     * 自然人实名登记
     */
    @PostMapping("/zrrSmdj")
    @ApiAccess
    public Result zrrSmdj(@Validated @RequestBody ZrrSmdjReq req) {
        return this.apiZrrService.zrrSmdj(req);
    }

    /**
     * 银行卡验证
     */
    @PostMapping("/bankCardVerify")
    @ApiAccess
    public Result bankCardVerify(@Validated @RequestBody BankCardVerifyReq req) {

        UnionPayBankCardVerifyRequestDTO request = new UnionPayBankCardVerifyRequestDTO();
        request.setOutRequestId(req.getOutRequestId());
        request.setPhoneNo(req.getPhoneNo());
        request.setCertType(req.getCertType());
        request.setCertNo(req.getCertNo());
        request.setName(req.getName());
        request.setCardNo(req.getCardNo());
        return Result.ok(yljyCommonClient.bankCardVerify(request));
    }

    /**
     * 人脸H5验证
     */
    @PostMapping("/getFaceVerify")
    @ApiAccess
    public Result getFaceVerify(@Validated @RequestBody GetFaceVerifyReq req) {
        UnionPayFaceH5VerifyRequestDTO request = new UnionPayFaceH5VerifyRequestDTO();

        final String key = "rlynjghdUrl:"+req.getOutRequestId();
        RedisUtils.put(key,req.getRlynjghddz());
        request.setOutRequestId(req.getOutRequestId());
        request.setCertNo(req.getCertNo());
        request.setName(req.getName());
        request.setReturnUrl(req.getReturnUrl());
        request.setType("action");
        request.setReqSn(req.getReqSn());
        return Result.ok(yljyCommonClient.faceH5Verify(request));
    }

    /**
     * 人脸H5验证结果查询
     */
    @PostMapping("/queryFaceVerifyResult")
    @ApiAccess
    public Result queryFaceVerifyResult(@Validated @RequestBody  QueryFaceVerifyResultReq req){
        FaceH5VerifyResultQueryRequestDTO request = new FaceH5VerifyResultQueryRequestDTO();
        request.setOutRequestId(req.getOutRequestId());
        request.setReqSn(req.getReqSn());
        return Result.ok(yljyCommonClient.faceH5VerifyResultQuery(request));
    }

    /**
     * 人脸H5验证结果查询
     */
    @PostMapping("/faceH5VerifyResultNotify")
    @ApiAccess
    public Result faceH5VerifyResultNotify(@Validated @RequestBody  FaceH5VerifyResultNotifyReq req){
        final String key = "rlynjghdUrl:"+req.getOutRequestId();
        final String url = RedisUtils.get(key);
        RedisUtils.delete(key);
        FaceH5VerifyResultNotifyRequestDTO request = new FaceH5VerifyResultNotifyRequestDTO();
        request.setOutRequestId(req.getOutRequestId());
        request.setReqSn(req.getReqSn());
        request.setBizResult(req.getBizResult());
        req.setCertNo(req.getCertNo());
        req.setName(req.getName());
        req.setPic(req.getPic());
        return Result.ok(rlsbCallbackCommonClient.faceH5VerifyResultNotify(url,request));
    }


}
